Shutdown Windows
You can Shutdown, Reboot, or logoff Windows using ExitWindowsEx API function. Example:
ExitWindowsEx(EWX_REBOOT, 0);
ExitWindowsEx(EWX_SHUTDOWN, 0);
when call above functions Windows will prompt you in closing some applications that
need confirmation messages such as (Close without save?). To force shutdown or reboot
use EWX_FORCE flag, example:
ExitWindowsEx(EWX_REBOOT + EWX_FORCE, 0);
This will work fine in Windows 9x family, but it will not work in WinNT, or Windows
2000. In WinNT you need to set privileges that enables you to shutdown or reboot
Windows. Use below function to add privilege:
function AddPrivilege(privilegeName: string) : boolean;
var c1,c2 : cardinal;
i64 : int64;
tp1,tp2 : TTokenPrivileges;
Ret: Cardinal;
begin
result:=false;
if OpenProcessToken(windows.GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,c1)
then
try
if LookupPrivilegeValue(nil,pchar(privilegeName),i64) then begin
tp1.PrivilegeCount:=1;
tp1.Privileges[0].Luid:=i64;
tp1.Privileges[0].Attributes:=0;
if AdjustTokenPrivileges(c1,false,tp1,sizeOf(TTokenPrivileges),tp2,c2) then
begin
if (c2=sizeOf(TTokenPrivileges)) and (tp2.PrivilegeCount=1) and (tp2.Privileges[0].Luid=i64)
then
tp1.Privileges[0].Attributes:=tp2.Privileges[0].Attributes;
tp1.Privileges[0].Attributes:=tp1.Privileges[0].Attributes or SE_PRIVILEGE_ENABLED;
result:=AdjustTokenPrivileges(c1,false,tp1,sizeOf(TTokenPrivileges),PTokenPrivileges(nil)^,Ret);
end;
end;
finally CloseHandle(c1) end;
end;
Example:
AddPrivilege('SeShutdownPrivilege');
ExitWindowsEx(EWX_Force + EWX_REBOOT, 0);